package com.hfuuwzy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hfuuwzy.entity.User;
import com.hfuuwzy.mapper.UserMapper;
import com.hfuuwzy.service.UserService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    //    @Autowired
//    private UserMapper;
    @Autowired
    private UserService userService;

    // 新增或者修改
    @PostMapping
    public boolean save(@RequestBody User user) {
        return userService.saveUser(user);
    }

    // 查询所有数据
    @GetMapping
    public List<User> getAll() {
        return userService.list();
    }

    // 删除数据
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Integer id) {
        return userService.removeById(id);
    }

    // 批量删除
    @PostMapping("/del/batch")
    public boolean deleteBatch(@RequestBody List<Integer> ids) {
        return userService.removeBatchByIds(ids);
    }


//    // 分页查询 RequestParam 接收 ?pageNum&pageSize
//    @GetMapping("/page")
//    public Map<String, Object> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String username) {
//        pageNum = (pageNum - 1) * pageSize;
//        List<User> data = userMapper.selectPage(pageNum, pageSize, username);
//        Integer total = userMapper.selectTotal(username);
//        Map<String, Object> res = new HashMap<>();
//        res.put("data", data);
//        res.put("total", total);
//        return res;
//    }
//    @GetMapping("/EmailPage")
//    public Map<String, Object> findEmailPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String email) {
//        pageNum = (pageNum - 1) * pageSize;
//        List<User> data = userMapper.selectEmailPage(pageNum, pageSize, email);
//        Integer total = userMapper.selectEmailTotal(email);
//        Map<String, Object> res = new HashMap<>();
//        res.put("data", data);
//        res.put("total", total);
//        return res;
//    }

    @GetMapping("/page")
    public IPage<User> findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam(defaultValue = "") String username,
                                @RequestParam(defaultValue = "") String email,
                                @RequestParam(defaultValue = "") String address) {
        IPage<User> page = new Page<>(pageNum, pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like(Strings.isNotEmpty(username),User::getUsername());
        if (!"".equals(username)) {
            queryWrapper.like("username", username);

        }
        if (!"".equals(email)) {
            queryWrapper.like("email", email);

        }
        if (!"".equals(address)) {
            queryWrapper.like("address", address);

        }
//        queryWrapper.or().like("username", username); 加入 OR
        return userService.page(page, queryWrapper);
    }
}
